import BlockManager, { BlockType, IBlock, blockTypeVals } from "./BlockManager";
import Utils from "../common/Util";
import GameManager from "../common/GameManager";
import PropManager from "./PropManager";
import GameData from "../common/GameData";

// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class Block extends cc.Component {
    @property
    index: number = -1;

    private _type: BlockType = BlockType.none;
    private _gameData: GameData;
    onLoad() {
        this._gameData = GameManager.getInstance().gameData;
        this.node.scale = 0.7;
    }

    onEnable() {
        BlockManager.getInstance().on("update", (data) => {
            if (data.index == this.index) {
                this.updateHandler();
            }
        }, this)
    }

    onDisable() {

    }

    start() {
        
    }

    public updateHandler(): void {

    }


    update(dt) {
        let block: IBlock = BlockManager.getInstance().getBlockByIndex(this.index);
        block.node.scale = 0.7;
        if (this._type == block.type) {
            return;
        }
        if(this._type == BlockType.none) {
            Utils.addAutoRemoveDragonBones(block.node, "prefab/PlaceNode", "newAnimation", () => {
            }, this, 128);
        }
        let blockManager: BlockManager = BlockManager.getInstance();
        this._type = block.type;
        if (block.node) {
            block.node.removeAllChildren();
            block.node.opacity = 255;
        }
        if (block.type == BlockType.none) {
            block.dirty = false;
        } else if (block.node) {
            let node: cc.Node = blockManager.getGridNodeByType(block.type);
            node.parent = block.node;
            if (this._type == BlockType.HT_2048) {
                block.neighbor.forEach(element => {
                    if (blockManager.blockArr[element].type > 0) {
                        blockManager.blockArr[element].dirty = true;
                    }
                })
                this._gameData.AddHexagon(block.type, 0);
                GameManager.getInstance().audioManager.PlaySoundEffect1("bomb");
                Utils.addAutoRemoveDragonBones(block.node, "prefab/Bomb2048Node", "newAnimation", () => {
                    block.type = BlockType.none;
                    block.neighbor.forEach(element => {
                        if (blockManager.blockArr[element].type > 0) {
                            let addScore: number = blockTypeVals[blockManager.blockArr[element].type] * 3;
                            this._gameData.AddCurScore(addScore);
                            GameManager.getInstance().eventCenter.DispatchEvent("block_score_update", { pos: blockManager.blockArr[element].pos, score: addScore });
                            /*Utils.addAutoRemoveDragonBone(blockManager.blockArr[element].node, "prefab/BombNode", "2048boom", () => {
                            }, this, () => {
                                blockManager.blockArr[element].node.runAction(cc.sequence(cc.fadeOut(.1), cc.callFunc(() => {
                                    blockManager.blockArr[element].type = 0;
                                })))
                            }); */
                            Utils.addAutoRemoveDragonBones(blockManager.blockArr[element].node, "prefab/Bomb2048Node", "newAnimation", () => {
                                blockManager.blockArr[element].type = 0;
                            }, this);
                            this._gameData.AddHexagon(blockManager.blockArr[element].type, 0);
                        }
                    });
                }, this);
                if(block.isProp){
                    block.isProp = false;
                }else{
                    setTimeout(() => {
                        PropManager.getInstance().openTreasure();
                    }, 2000);
                }
            }
        } else {
            block.dirty = false;
        }
    }
}